//
//  UIButton+Extension.m
//  GongDan
//
//  Created by 了凡 on 16/1/20.
//  Copyright © 2016年 河北广联. All rights reserved.
//

#import "UIButton+Extension.h"

@implementation UIButton (Extension)

/**
 *   title button
 *
 */
+ (instancetype)buttonWithType:(UIButtonType)buttonType
                          font:(UIFont *)font
                         frame:(CGRect)frame
                         title:(NSString *_Nullable)title
                    titleColor:(UIColor *_Nullable)color {
    return [self buttonWithType:buttonType
                           font:font
                          frame:frame
                          title:title
                    selectTitle:@""
                     titleColor:color
                    selectColor:nil];
}

/**
 *   title 变色 button
 *
 */
+ (instancetype)buttonWithType:(UIButtonType)buttonType
                          font:(UIFont *)font
                         frame:(CGRect)frame
                         title:(NSString *)title
                   selectTitle:(NSString *)sTitle
                    titleColor:(UIColor *)color
                   selectColor:(UIColor *)sColor {
    return [self buttonWithType:buttonType
                           font:font
                          frame:frame
                          title:title
                    selectTitle:sTitle
                     titleColor:color
                    selectColor:sColor
                          image:nil
                      selectImg:nil];
}

/**
 *  Img 改变 button
 *
 */
+ (instancetype)buttonWithType:(UIButtonType)buttonType
                         frame:(CGRect)frame
                         image:(NSString *)imageStr
                     selectImg:(NSString *)sImgStr {
    return [self buttonWithType:buttonType
                           font:KFontSize(16.0f)
                          frame:frame
                          title:@""
                    selectTitle:@""
                     titleColor:nil
                    selectColor:nil
                          image:imageStr
                      selectImg:sImgStr];
}

/**
 *    Img + title button
 *
 */
+ (instancetype)buttonWithType:(UIButtonType)buttonType
                          font:(UIFont *)font
                         frame:(CGRect)frame
                         title:(nullable NSString *)title
                    titleColor:(nullable UIColor *)color
                         image:(nullable NSString *)imageStr {
    return [self buttonWithType:buttonType
                           font:font
                          frame:frame
                          title:title
                    selectTitle:title
                     titleColor:color
                    selectColor:color
                          image:imageStr
                      selectImg:imageStr];
}

/**
 *   Img + title 改变 button
 *
 */
+ (instancetype)buttonWithType:(UIButtonType)buttonType
                          font:(UIFont *_Nullable)font
                         frame:(CGRect)frame
                         title:(NSString *_Nullable)title
                   selectTitle:(NSString *_Nullable)sTitle
                    titleColor:(UIColor *_Nullable)color
                   selectColor:(UIColor *_Nullable)sColor
                         image:(NSString *_Nullable)imageStr
                     selectImg:(NSString *_Nullable)sImgStr {
    UIButton *button = [UIButton buttonWithType:buttonType];
    if (font) {
        button.titleLabel.font = font;
    }
    button.frame = frame;
    
    if (title && title.length > 0) {
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (sTitle && sTitle.length > 0) {
        [button setTitle:sTitle forState:UIControlStateSelected];
    }
    if (color) {
        [button setTitleColor:color forState:UIControlStateNormal];
    }
    if (sColor) {
        [button setTitleColor:sColor forState:UIControlStateSelected];
    }
    if (imageStr &&imageStr.length > 0) {
        [button setImage:[ImageNamed(imageStr) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    }
    if (sImgStr && sImgStr.length > 0) {
        [button setImage:[ImageNamed(sImgStr) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
    }
    return button;
}

+ (UIButton *)buttonWithTextColor:(UIColor *)textColor font:(CGFloat)textFont backgroudColor:(UIColor *)backColor text:(NSString *)text Target:(id)target Selector:(SEL)selector{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:text forState:UIControlStateNormal];
    if (backColor != nil) {
        button.backgroundColor = backColor;
    }
    button.titleLabel.font = [UIFont systemFontOfSize:textFont];
    if (textColor != nil) {
        [button setTitleColor:textColor forState:UIControlStateNormal];
    }
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return button;
}

+ (UIButton *)buttonWithTextColor:(UIColor *)textColor font:(CGFloat)textFont backgroudColor:(UIColor *)backColor image:(UIImage *)image imageIsLeft:(BOOL)isLeft text:(NSString *)text Target:(id)target Selector:(SEL)selector{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:text forState:UIControlStateNormal];
    if (backColor != nil) {
        button.backgroundColor = backColor;
    }
    button.titleLabel.font = [UIFont systemFontOfSize:textFont];
    if (textColor != nil) {
        [button setTitleColor:textColor forState:UIControlStateNormal];
    }
    [button setImage:image forState:UIControlStateNormal];
    
    if (isLeft && (image != nil)) {
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    }else if((isLeft == NO) && (image != nil)){
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }

    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return button;
}

/**
 按钮倒计时
 
 @param timeout 时间
 @param tittle 按钮标题
 @param waitTittle 等待标题
 */
-(void)startTime:(NSInteger )timeout title:(NSString *)tittle waitTittle:(NSString *)waitTittle{
    __block NSInteger timeOut=timeout; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeOut<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self setTitle:tittle forState:UIControlStateNormal];
                self.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeOut % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"____%@",strTime);
                [self setTitle:[NSString stringWithFormat:@"%@%@",strTime,waitTittle] forState:UIControlStateNormal];
                self.userInteractionEnabled = NO;
                
            });
            timeOut--;
            
        }
    });
    dispatch_resume(_timer);
}


- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect bounds = self.bounds;
    //若原热区小于44x44，则放大热区，否则保持原大小不变
    CGFloat widthDelta = MAX(44.0 - bounds.size.width, 0);
    CGFloat heightDelta = MAX(44.0 - bounds.size.height, 0);
    bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
    return CGRectContainsPoint(bounds, point);
}

- (void)layoutButtonWithEdgeInsetsStyle:(CSButtonEdgeInsetsStyle)style ImageTitleSpace:(CGFloat)space
{
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = self.imageView.frame.size.width;
    CGFloat imageHeight = self.imageView.frame.size.height;
    
    CGFloat labelWidth = 0.0;
    CGFloat labelHeight = 0.0;
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
        // 由于iOS8中titleLabel的size为0，用下面的这种设置
        labelWidth = self.titleLabel.intrinsicContentSize.width;
        labelHeight = self.titleLabel.intrinsicContentSize.height;
    } else {
        labelWidth = self.titleLabel.frame.size.width;
        labelHeight = self.titleLabel.frame.size.height;
    }
    
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    
    // 3. 根据style和space得到imageEdgeInsets和labelEdgeInsets的值
    switch (style) {
        case CSButtonEdgeInsetsStyleTop:
        {
            imageEdgeInsets = UIEdgeInsetsMake(-labelHeight-space/2.0, 0, 0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith, -imageHeight-space/2.0, 0);
        }
            break;
        case CSButtonEdgeInsetsStyleLeft:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, -space/2.0, 0, space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, space/2.0, 0, -space/2.0);
        }
            break;
        case CSButtonEdgeInsetsStyleBottom:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, 0, -labelHeight-space/2.0, -labelWidth);
            labelEdgeInsets = UIEdgeInsetsMake(-imageHeight-space/2.0, -imageWith, 0, 0);
        }
            break;
        case CSButtonEdgeInsetsStyleRight:
        {
            imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+space/2.0, 0, -labelWidth-space/2.0);
            labelEdgeInsets = UIEdgeInsetsMake(0, -imageWith-space/2.0, 0, imageWith+space/2.0);
        }
            break;
        default:
            break;
    }
    // 4. 赋值
    self.titleEdgeInsets = labelEdgeInsets;
    self.imageEdgeInsets = imageEdgeInsets;
}


@end
